package com.snoob.springboot.enterprise.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.snoob.springboot.base.BaseException;
import com.snoob.springboot.base.BasePageResult;
import com.snoob.springboot.enterprise.entity.EnterpriseEntity;
import com.snoob.springboot.enterprise.mapper.EnterpriseMapper;
import com.snoob.springboot.enterprise.service.EnterpriseService;
import com.snoob.springboot.pipleline.qo.SelectEnterpriseQo;
import com.snoob.springboot.pipleline.vo.DeleteEnterpriseVoReq;
import com.snoob.springboot.util.StrUtil;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 * 企业 服务实现类
 * </p>
 *
 * @author lixing_java@163.com
 * @since 2022-05-14
 */
@Service
public class EnterpriseServiceImpl extends ServiceImpl<EnterpriseMapper, EnterpriseEntity> implements EnterpriseService {
    /**
     * @描述 添加企业信息
     * @作者 lixing_java@163.com
     * @日期 2022/5/14 19:22
     */
    @Override
    public String insertEnterprise(EnterpriseEntity entity) {
        // 非空校验
        if (null == entity) {
            throw new BaseException("参数缺失");
        }
        // 必填字段校验
        if (!StrUtil.isEffective(entity.getEnterpriseName())) {
            throw new BaseException("企业名称缺失");
        } else {
            // 唯一字段校验
            EnterpriseEntity checkEnterpriseEntity = this.getOne(new QueryWrapper<EnterpriseEntity>().eq("enterprise_name", entity.getEnterpriseName()));
            if (null != checkEnterpriseEntity) {
                throw new BaseException("该企业名称已经存在");
            }
        }
        // 添加
        entity.setEnterpriseCode(UUID.randomUUID().toString()); // 随机生成UUID为企业编码
        entity.setCreateTime(new Date());
        boolean enterpriseEntityFlag = this.save(entity);
        return enterpriseEntityFlag ? entity.getEnterpriseCode() : null;
    }

    /**
     * @描述 编辑企业信息
     * @作者 lixing_java@163.com
     * @日期 2022/5/14 19:22
     */
    @Override
    public Boolean updateEnterprise(EnterpriseEntity entity) {
        // 非空校验
        if (null == entity) {
            throw new BaseException("参数缺失");
        }
        // 必填字段校验
        EnterpriseEntity checkEnterpriseEntity = null;
        if (!StrUtil.isEffective(entity.getEnterpriseCode())) {
            throw new BaseException("企业编码缺失");
        } else {
            // 唯一字段校验
            checkEnterpriseEntity = this.getOne(new QueryWrapper<EnterpriseEntity>().eq("enterprise_code", entity.getEnterpriseCode()));
            if (null == checkEnterpriseEntity) {
                throw new BaseException("企业编码非法");
            }
        }
        if (!StrUtil.isEffective(entity.getEnterpriseName())) {
            throw new BaseException("企业名称缺失");
        } else {
            // 唯一字段校验
            if (!entity.getEnterpriseName().equals(checkEnterpriseEntity.getEnterpriseName())) {
                checkEnterpriseEntity = this.getOne(new QueryWrapper<EnterpriseEntity>().eq("enterprise_name", entity.getEnterpriseName()));
                if (null != checkEnterpriseEntity) {
                    throw new BaseException("该企业名称已经存在");
                }
            }
        }
        // 更新
        boolean updateEnterpriseFlag = this.update(entity, new UpdateWrapper<EnterpriseEntity>().eq("enterprise_code", entity.getEnterpriseCode()));
        return updateEnterpriseFlag;
    }

    /**
     * @描述 删除企业信息
     * @作者 lixing_java@163.com
     * @日期 2022/5/14 19:22
     */
    @Override
    public Boolean deleteEnterprise(DeleteEnterpriseVoReq vo) {
        // 非空校验
        List<String> enterpriseCodeList = vo.getEnterpriseCodeList();
        if (enterpriseCodeList.isEmpty()) {
            throw new BaseException("企业编码缺失");
        }
        //
        QueryWrapper<EnterpriseEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("enterprise_code", enterpriseCodeList);
        boolean deleteEnterpriseFlag = this.remove(queryWrapper);
        return deleteEnterpriseFlag;
    }

    /**
     * @描述 查询企业信息
     * @作者 lixing_java@163.com
     * @日期 2022/5/14 19:22
     */
    @Override
    public EnterpriseEntity selectEnterprise(SelectEnterpriseQo qo) {
        // 非空校验
        if (!StrUtil.isEffective(qo.getEnterpriseCode()) && !StrUtil.isEffective(qo.getId().toString())) {
            throw new BaseException("参数缺失");
        }
        // 条件参数
        QueryWrapper<EnterpriseEntity> entityQueryWrapper = new QueryWrapper<>();
        if (null != qo.getId() && StrUtil.isEffective(qo.getId().toString())) {
            entityQueryWrapper.eq("id", qo.getId());
        }
        if (StrUtil.isEffective(qo.getEnterpriseCode())) {
            entityQueryWrapper.eq("enterprise_code", qo.getEnterpriseCode());
        }
        if (StrUtil.isEffective(qo.getEnterpriseName())) {
            entityQueryWrapper.eq("enterprise_name", qo.getEnterpriseName());
        }
        // 查询
        EnterpriseEntity enterpriseEntity = this.getOne(entityQueryWrapper);
        return enterpriseEntity;
    }

    /**
     * @描述 分页查询企业信息
     * @作者 lixing_java@163.com
     * @日期 2022/5/14 19:22
     */
    @Override
    public BasePageResult<EnterpriseEntity> selectEnterpriseByPage(SelectEnterpriseQo qo) {
        // 非空校验
        if (!StrUtil.isEffective(qo.getEnterpriseCode()) && !StrUtil.isEffective(qo.getId().toString())) {
            throw new BaseException("参数缺失");
        }
        // 条件参数
        QueryWrapper<EnterpriseEntity> entityQueryWrapper = new QueryWrapper<>();
        if (StrUtil.isEffective(qo.getEnterpriseCode())) {
            entityQueryWrapper.eq("enterprise_code", qo.getEnterpriseCode());
        }
        if (StrUtil.isEffective(qo.getEnterpriseName())) {
            entityQueryWrapper.likeLeft("enterprise_name", qo.getEnterpriseName());
        }
        if (null != qo.getBaseCreateTimeStart()) {
            entityQueryWrapper.ge("create_time", qo.getBaseCreateTimeStart());
        }
        if (null != qo.getBaseCreateTimeEnd()) {
            entityQueryWrapper.le("create_time", qo.getBaseCreateTimeEnd());
        }
        // 分页查询
        Page<EnterpriseEntity> page = new Page<EnterpriseEntity>(qo.getBasePageNum(), qo.getBasePageSize());
        page = this.page(page, entityQueryWrapper);
        return new BasePageResult<EnterpriseEntity>(page);
    }
}
